<?php

namespace app\api\controller;
use ba\Tree;
use Throwable;
use think\facade\Request;
use think\facade\Db;
use app\admin\model\ComplaintReply;
use app\admin\model\Message as MessageModel;
use app\api\validate\Message as MessageValidate;
use app\common\controller\Api;
class Message extends Api
{	
    /**
     * 投诉列表
     * author：mozi
     * time:2025.05.17
     * */
     public function lists(){
          $uid = $this->request->post("uid");
          if(isset($uid) && $uid != ''){
               $res = MessageModel::where("uid",$uid)->order("id desc")->select();
               return json(['code'=>200,'msg'=>'ok','data'=>$res]);      
          }else{
               return json(['code'=>100,'msg'=>'手机号码不能为空']);      
          }
     }

     /**
     * 获取单个投诉详细
     * author：mozi
     * time:2025.05.17
     * */
     public function details(){
          $id = $this->request->post("id");
          if($id){
               //$w['uid'] = request()->userId;
               $w['id'] = $id;
               $res = MessageModel::where($w)->find();
               if(isset($res['imgs']) && !empty($res['imgs'])){
                 
                    $res['imgs'] = explode(",",$res['imgs']);
               }
               return json(['code'=>200,'msg'=>'ok','data'=>$res ?? []]);      
          }else{
               return json(['code'=>100,'msg'=>'无记录']);      
          }
     }


     /**
     * 投诉回复
     * author：mozi
     * time:2025.05.17
     * */
     public function reply(){
          $rid = $this->request->post("rid",0);
          $is_end = $this->request->post("is_end",0);
          $txt = $this->request->post("txt",'');
          if($rid){
               $find = MessageModel::where("id",$rid)->where('agent_id',request()->adminId)->find();
               if(!$find){
                      return json(['code'=>100,'msg'=>'Auth error!']);       
               }
               if($find['status'] == 1){
                      return json(['code'=>100,'msg'=>'该反馈已处理，回复通道已关闭']);       
               }
               $data['remarks'] = $txt;
               $data['uid'] = request()->userId;
               $data['rid'] = $rid;
               $data['agent_id'] = request()->adminId;
               $res = MessageModel::create($data);
               if($res && $is_end == 1){
                    MessageModel::where("id",$rid)->update(['status'=>1]);
               }
               return json(['code'=>200,'msg'=>'ok']);      
          }else{
               return json(['code'=>100,'msg'=>'数据异常']);      
          }
     }


	 /**
     * 用户留言
     * author：mozi
     * time:2025.05.16
     * */
	 public function sub(){
     	   $params = $this->request->post("");
     	   $validate = new MessageValidate();
             try {
                 $validate->check($params);
             } catch (Throwable $e) {
                 return json(['code'=>100,'msg'=>$e->getMessage()]);
             }
             $model = new MessageModel();

             $todayStart = strtotime('today');
             $todayEnd = strtotime('tomorrow') - 1;
             $find  = $model->where('timestamp', '>=', $todayStart)->where('timestamp', '<=', $todayEnd)->where(['ordernum'=>$params['ordernum']])->count();
             if($find){
                  //$this->error("该订单您已留言！");
             }

             $model->startTrans();
             try {

                 $model->save($params);
                 $model->commit();
             } catch (Throwable $e) {
                 $model->rollback();
                 $this->error($e->getMessage());
             }
     		return json(['code'=>200,'msg'=>'ok']);		
	 }
}